package managerSIP;
import java.io.IOException;

import net.sf.asterisk.manager.AuthenticationFailedException;
import net.sf.asterisk.manager.ManagerConnection;
import net.sf.asterisk.manager.ManagerConnectionFactory;
import net.sf.asterisk.manager.ManagerEventHandler;
import net.sf.asterisk.manager.TimeoutException;
import net.sf.asterisk.manager.action.StatusAction;
import net.sf.asterisk.manager.event.ManagerEvent;
import net.sf.asterisk.manager.event.MeetMeJoinEvent;
import net.sf.asterisk.manager.event.MeetMeLeaveEvent;
import net.sf.asterisk.manager.event.MeetMeStopTalkingEvent;
import net.sf.asterisk.manager.event.MeetMeTalkingEvent;

public class Events extends Thread implements ManagerEventHandler 
{
    private ManagerConnection managerConnection;

    private Manager myManager;
    //<BETA>
    //New constructor receiving parameters
    
    public Events(Manager mgr, String hostname, String username, String password) throws
        IOException {
    	myManager = mgr;
      // connect to Asterisk  	
      try {
        ManagerConnectionFactory factory = new ManagerConnectionFactory();
        this.managerConnection = factory.getManagerConnection(hostname, username,
            password);
        //get reference to the object who wants to be notified of events
      }
      catch (Exception e) {
        e.printStackTrace();
      }
      
    }
    
    //</BETA>


    public Events()
    {    	
        
    }

    public void run()
    {
                // connect to Asterisk and log in
        try {
        	managerConnection.setSocketTimeout(30000);
			managerConnection.login();
			// register for events
	        managerConnection.addEventHandler(this);
	        managerConnection.sendAction(new StatusAction());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AuthenticationFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/*
        // wait 1 second for events to come in
        try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/

        // and finally log off and disconnect
        //managerConnection.logoff();
    }


    public void handleEvent(ManagerEvent event)
    {
    	if (event instanceof MeetMeJoinEvent)
    	{
    		MeetMeJoinEvent meetmeevent = (MeetMeJoinEvent) event;
    		
        	System.out.println(meetmeevent.getMeetMe());
        	System.out.println(meetmeevent.getChannel());
        	System.out.println(meetmeevent.getUserNum().toString());
        	System.out.println(meetmeevent.getPrivilege().toString());
        	System.out.println(meetmeevent.getUniqueId().toString());
        	System.out.println(meetmeevent.getDateReceived().toString());
    		 
    		//<BETA>
    		//Notify the ManagerListener of this event
    		//???? getUserNum is a number???
    		// ??? How can I get user ID???    		
    		myManager.userJoinedConference(meetmeevent.getMeetMe(),
    				meetmeevent.getUserNum().toString(), meetmeevent.getChannel());
        	synchronized (this) {
        		this.notify();
        	}

    	}

    	if (event instanceof MeetMeLeaveEvent)
        {
    		MeetMeLeaveEvent meetmeevent = (MeetMeLeaveEvent) event;
    		
        	System.out.println(meetmeevent.getMeetMe());
        	System.out.println(meetmeevent.getChannel());
        	System.out.println(meetmeevent.getUserNum().toString());
        	System.out.println(meetmeevent.getPrivilege().toString());
        	System.out.println(meetmeevent.getUniqueId().toString());
        	System.out.println(meetmeevent.getDateReceived().toString());
        	
    		//<BETA>
            //Notify the ManagerListener of this event
            //???? getUserNum is a number???
            // ??? How can I get user ID???
            myManager.userLeaveConference(meetmeevent.getMeetMe(),
                    meetmeevent.getUserNum().toString(),meetmeevent.getChannel());
        	synchronized (this) {
        		this.notify();
        	}

        }
    	
    	if (event instanceof MeetMeTalkingEvent)
        {
    		MeetMeTalkingEvent meetmeevent = (MeetMeTalkingEvent) event;
        	System.out.println(meetmeevent.getMeetMe());
        	System.out.println(meetmeevent.getChannel());
        	System.out.println(meetmeevent.getUserNum().toString());
        	System.out.println(meetmeevent.getPrivilege().toString());
        	System.out.println(meetmeevent.getUniqueId().toString());
        	System.out.println(meetmeevent.getDateReceived().toString());
        	myManager.userTalkingEvent(meetmeevent.getMeetMe(),
                    meetmeevent.getUserNum().toString(),meetmeevent.getChannel());
        }
    	
    	if (event instanceof MeetMeStopTalkingEvent)
        {
    		MeetMeStopTalkingEvent meetmeevent = (MeetMeStopTalkingEvent) event;
        	System.out.println(meetmeevent.getMeetMe());
        	System.out.println(meetmeevent.getChannel());
        	System.out.println(meetmeevent.getUserNum().toString());
        	System.out.println(meetmeevent.getPrivilege().toString());
        	System.out.println(meetmeevent.getUniqueId().toString());
        	System.out.println(meetmeevent.getDateReceived().toString());
        	myManager.userStopTalkingEvent(meetmeevent.getMeetMe(),
                    meetmeevent.getUserNum().toString(),meetmeevent.getChannel());
        }
    }

}
